﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using KeaoBase;

namespace KeaoMDS.InfoManage
{
    public partial class WuLiaoXingHaoBianJi : BaseForm
    {
        private string m_WuLiaoMingCheng = string.Empty;
        private string m_WuLiaoBianHao = string.Empty;
        private string m_WuLiaoLeiXing = string.Empty;
        private DataGridViewRow m_SelectRow = null;
        public WuLiaoXingHaoBianJi(string WuLiaoMingCheng,string WuLiaoBianHao,string WuLiaoLeiXing)
        {
            InitializeComponent();
            m_WuLiaoBianHao = WuLiaoBianHao;
            m_WuLiaoMingCheng = WuLiaoMingCheng;
            m_WuLiaoLeiXing = WuLiaoLeiXing;
        }

        public WuLiaoXingHaoBianJi(string WuLiaoMingCheng, string WuLiaoBianHao,string WuLiaoLeiXing,DataGridViewRow SelectRow)
        {
            InitializeComponent();
            m_WuLiaoBianHao = WuLiaoBianHao;
            m_WuLiaoMingCheng = WuLiaoMingCheng;
            m_WuLiaoLeiXing = WuLiaoLeiXing;
            m_SelectRow = SelectRow;
        }

        private void WuLiaoXingHaoBianJi_Load(object sender, EventArgs e)
        {
            if (!string.Format("{0}", KeaoUtils._CurrentUserRow["juesequanxian"]).Contains("偏差设置"))
            {
                groupBox1.Visible = false;
            }
            BindCmd();
    
            txt_wuliaomingcheng.Text = m_WuLiaoMingCheng;
            if (m_SelectRow != null)
            {
                txt_xinghao.Text = string.Format("{0}", m_SelectRow.Cells["col_xinghao"].Value);
                txt_pianchazhongliang.Text = string.Format("{0}", m_SelectRow.Cells["col_pianchazhongliang"].Value);
                txt_huansuangongshi.Text = string.Format("{0}", m_SelectRow.Cells["col_huansuangongshi"].Value);
                chk_pianchazhuangtai.Checked = m_SelectRow.Cells["col_pianchazhuangtai"].Value.Equals("启用") ? true : false;
                txt_morenkouzhong.Text = string.Format("{0}", m_SelectRow.Cells["col_morenkouzhong"].Value);
                rb_xitong_putongkouzhong.Checked = m_SelectRow.Cells["col_kouzhongfangshi"].Value.Equals("普通扣重") ? true : false;
                cmb_hunningtu.Text = string.Format("{0}", m_SelectRow.Cells["col_hunningtu"].Value);
                cmb_liqingliao.Text = string.Format("{0}", m_SelectRow.Cells["col_liqingliao"].Value);
                cmb_shuiwenliao.Text = string.Format("{0}", m_SelectRow.Cells["col_shuiwenliao"].Value);
            }

            if (m_WuLiaoLeiXing == "出库物料")
            {
                cmb_hunningtu.Enabled = false;
                cmb_liqingliao.Enabled = false;
                cmb_shuiwenliao.Enabled = false;
            }
        }

        private void BindCmd()
        {
            string createCondition = KeaoUtils._DB.CreateAndCondition("fenzu", DataValueCommonType.字符串, "水稳料", "", SearchConditionType.等于);
            DataTable Dt1 = KeaoUtils._DB.GetDataTableByCondition("tbl_zidian", createCondition);
            DataRow newRow1 = Dt1.NewRow();
            newRow1["xiangmu"] = "—不关联—";
            newRow1["id"] = "";
            Dt1.Rows.InsertAt(newRow1, 0);
            cmb_shuiwenliao.DisplayMember = "xiangmu";
            cmb_shuiwenliao.ValueMember = "xiangmu";
            cmb_shuiwenliao.DataSource = Dt1;

            createCondition = KeaoUtils._DB.CreateAndCondition("fenzu", DataValueCommonType.字符串, "混凝土", "", SearchConditionType.等于);
            DataTable Dt2 = KeaoUtils._DB.GetDataTableByCondition("tbl_zidian", createCondition);
            DataRow newRow2 = Dt2.NewRow();
            newRow2["xiangmu"] = "—不关联—";
            newRow2["id"] = "";
            Dt2.Rows.InsertAt(newRow2, 0);
            cmb_hunningtu.DisplayMember = "xiangmu";
            cmb_hunningtu.ValueMember = "xiangmu";
            cmb_hunningtu.DataSource = Dt2;

            createCondition = KeaoUtils._DB.CreateAndCondition("fenzu", DataValueCommonType.字符串, "沥青料", "", SearchConditionType.等于);
            DataTable Dt3 = KeaoUtils._DB.GetDataTableByCondition("tbl_zidian", createCondition);
            DataRow newRow3 = Dt3.NewRow();
            newRow3["xiangmu"] = "—不关联—";
            newRow3["id"] = "";
            Dt3.Rows.InsertAt(newRow3, 0);
            cmb_liqingliao.DisplayMember = "xiangmu";
            cmb_liqingliao.ValueMember = "xiangmu";
            cmb_liqingliao.DataSource = Dt3;
        }


        private void menu_btn_baocun_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txt_xinghao.Text))
            {
                KeaoUtils.ShowInfoMessage("物料型号不能为空，请重试。");
                return;
            }

            if (string.IsNullOrEmpty(txt_pianchazhongliang.Text) || !KeaoUtils.isNumber(txt_pianchazhongliang.Text))
            {
                KeaoUtils.ShowInfoMessage("偏差重量不能为空，设置为数字，请重试。");
                return;
            }

            if (string.IsNullOrEmpty(txt_morenkouzhong.Text) || !KeaoUtils.isNumber(txt_morenkouzhong.Text))
            {
                KeaoUtils.ShowInfoMessage("偏差重量不能为空，设置为数字，请重试。");
                return;
            }


            if (m_SelectRow == null)
            {
                string createCondition = KeaoUtils._DB.CreateAndCondition("xinghao", DataValueCommonType.字符串, txt_xinghao.Text, "", SearchConditionType.等于);
                createCondition += KeaoUtils._DB.CreateAndCondition("wuliaobianhao", DataValueCommonType.字符串, m_WuLiaoBianHao, "", SearchConditionType.等于);
                DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_xinghao", createCondition);
                if (Dt == null)
                {
                    return;
                }
                if (Dt.Rows.Count > 0)
                {
                    KeaoUtils.ShowInfoMessage("该型号已经存在，不能重复保存。");
                    return;
                }
                TableRow newRow = new TableRow("tbl_xinghao", KeaoUtils.GetGUID(), DBCtl.插入);
                newRow.DataCols.Add(new TableCol() { ColName = "xinghao", ValueMember = txt_xinghao.Text, ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "wuliaobianhao", ValueMember = m_WuLiaoBianHao, ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "pianchazhongliang", ValueMember = txt_pianchazhongliang.Text, ValueType = DataValueCommonType.数字 });
                newRow.DataCols.Add(new TableCol() { ColName = "pianchazhuangtai", ValueMember = chk_pianchazhuangtai.Checked ? "启用" : "禁用", ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "huansuangongshi", ValueMember = txt_huansuangongshi.Text, ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "morenkouzhong", ValueMember = txt_morenkouzhong.Text, ValueType = DataValueCommonType.数字 });
                newRow.DataCols.Add(new TableCol() { ColName = "kouzhongfangshi", ValueMember = rb_xitong_putongkouzhong.Checked ? "普通扣重" : "百分比扣重", ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "0", ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "zhandian", ValueMember = KeaoUtils.ConfigGetNodeAttr("客户", "站点"), ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "kehubianhao", ValueMember = KeaoUtils.ConfigGetNodeAttr("客户", "客户编号"), ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "hunningtu", ValueMember = cmb_hunningtu.Text, ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "liqingliao", ValueMember = cmb_liqingliao.Text, ValueType = DataValueCommonType.字符串 });
                newRow.DataCols.Add(new TableCol() { ColName = "shuiwenliao", ValueMember = cmb_shuiwenliao.Text, ValueType = DataValueCommonType.字符串 });
                if (KeaoUtils._DB.SaveTableRows(newRow) > 0)
                {
                    DialogResult = DialogResult.OK;
                }
            }
            else
            {
                string createCondition = KeaoUtils._DB.CreateAndCondition("xinghao", DataValueCommonType.字符串, txt_xinghao.Text, "", SearchConditionType.等于);
                createCondition += KeaoUtils._DB.CreateAndCondition("wuliaobianhao", DataValueCommonType.字符串, m_WuLiaoBianHao, "", SearchConditionType.等于);
                createCondition += KeaoUtils._DB.CreateAndCondition("id", DataValueCommonType.字符串, string.Format("{0}", m_SelectRow.Cells["col_xinghao_id"].Value), "", SearchConditionType.不等于);
                DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_xinghao", createCondition);
                if (Dt == null)
                {
                    return;
                }
                if (Dt.Rows.Count > 0)
                {
                    KeaoUtils.ShowInfoMessage("该型号已经存在，不能重复保存。");
                    return;
                }
                TableRow editRow = new TableRow("tbl_xinghao", string.Format("{0}", m_SelectRow.Cells["col_xinghao_id"].Value), DBCtl.修改);
                editRow.DataCols.Add(new TableCol() { ColName = "xinghao", ValueMember = txt_xinghao.Text, ValueType = DataValueCommonType.字符串 });
                editRow.DataCols.Add(new TableCol() { ColName = "wuliaobianhao", ValueMember = m_WuLiaoBianHao, ValueType = DataValueCommonType.字符串 });
                editRow.DataCols.Add(new TableCol() { ColName = "pianchazhongliang", ValueMember = txt_pianchazhongliang.Text, ValueType = DataValueCommonType.数字 });
                editRow.DataCols.Add(new TableCol() { ColName = "pianchazhuangtai", ValueMember = chk_pianchazhuangtai.Checked ? "启用" : "禁用", ValueType = DataValueCommonType.字符串 });
                editRow.DataCols.Add(new TableCol() { ColName = "huansuangongshi", ValueMember = txt_huansuangongshi.Text, ValueType = DataValueCommonType.字符串 });
                editRow.DataCols.Add(new TableCol() { ColName = "morenkouzhong", ValueMember = txt_morenkouzhong.Text, ValueType = DataValueCommonType.数字 });
                editRow.DataCols.Add(new TableCol() { ColName = "kouzhongfangshi", ValueMember = rb_xitong_putongkouzhong.Checked ? "普通扣重" : "百分比扣重", ValueType = DataValueCommonType.字符串 });
                editRow.DataCols.Add(new TableCol() { ColName = "shangchuanzhuangtai", ValueMember = "0", ValueType = DataValueCommonType.字符串 });
                editRow.DataCols.Add(new TableCol() { ColName = "hunningtu", ValueMember = cmb_hunningtu.Text, ValueType = DataValueCommonType.字符串 });
                editRow.DataCols.Add(new TableCol() { ColName = "liqingliao", ValueMember = cmb_liqingliao.Text, ValueType = DataValueCommonType.字符串 });
                editRow.DataCols.Add(new TableCol() { ColName = "shuiwenliao", ValueMember = cmb_shuiwenliao.Text, ValueType = DataValueCommonType.字符串 });

                if (KeaoUtils._DB.SaveTableRows(editRow) > 0)
                {
                    DialogResult = DialogResult.OK;
                }
            }
        }

        private void menu_btn_guanbi_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void rb_xitong_putongkouzhong_CheckedChanged(object sender, EventArgs e)
        {
            rb_xitong_baifenbikouzhong.Checked = !rb_xitong_putongkouzhong.Checked;
        }

        private void txt_morenkouzhong_Click(object sender, EventArgs e)
        {
            TextBox t = sender as TextBox;
            t.Focus();
            t.SelectAll();
        }



    }
}
